/*******************************************************************
 *
 * @Version : 0.5 
 *
 * Move.java 
 *
 * 2011-10-17 下午03:02:35
 *
 *******************************************************************/
package game.breeze.ani;

import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.AnimationAction;
import com.badlogic.gdx.scenes.scene2d.actions.ActionResetingPool;

/**
 * Class desc there
 * 
 * @author yangwei ywengineer@gmail.com
 * 
 */
public class Move extends AnimationAction {

	private static final ActionResetingPool<Move> pool = new ActionResetingPool<Move>(4, 100) {
		@Override
		protected Move newObject() {
			return new Move();
		}
	};

	protected OnActionProgress pl;
	protected float x;
	protected float y;
	protected float startX;
	protected float startY;
	protected float deltaX;
	protected float deltaY;

	public static Move $(float x, float y, float duration) {
		Move action = pool.obtain();
		action.x = x;
		action.y = y;
		action.duration = duration;
		action.invDuration = 1 / duration;
		return action;
	}

	public Action setProgressListener(final OnActionProgress listener) {
		this.pl = listener;
		return this;
	}

	@Override
	public void setTarget(Actor actor) {
		this.target = actor;
		this.startX = target.x;
		this.startY = target.y;
		this.deltaX = x - target.x;
		this.deltaY = y - target.y;
		this.taken = 0;
		this.done = false;
	}

	@Override
	public void act(float delta) {
		float alpha = createInterpolatedAlpha(delta);
		if (done) {
			target.x = x;
			target.y = y;
		} else {
			target.x = startX + deltaX * alpha;
			target.y = startY + deltaY * alpha;
			if (pl != null) {
				pl.progress(this);
			}
		}
	}

	@Override
	public void finish() {
		super.finish();
		pool.free(this);
	}

	@Override
	public Action copy() {
		Move moveTo = $(x, y, duration);
		if (interpolator != null)
			moveTo.setInterpolator(interpolator.copy());
		return moveTo;
	}
}
